package com.study.flink.batch;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.MapOperator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author stephen
 * @create 2019-01-28 21:05
 * @since 1.0.0
 */
public class BatchDemoBroadcast {

    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        // 准备需要广播的数据
        ArrayList<Tuple2<String, Integer>> data = new ArrayList<>();
        data.add(new Tuple2<>("s",11));
        data.add(new Tuple2<>("z",12));
        data.add(new Tuple2<>("x",13));

        DataSource<Tuple2<String, Integer>> source = env.fromCollection(data);

        // 将需要广播的数据转成map类型
        MapOperator<Tuple2<String, Integer>, Object> toBroadcast = source.map(new MapFunction<Tuple2<String, Integer>, Object>() {
            @Override
            public HashMap<String, Integer> map(Tuple2<String, Integer> value) throws Exception {
                HashMap<String, Integer> map = new HashMap<>();
                map.put(value.f0, value.f1);
                return map;
            }
        });

        //待处理数据
        DataSource<String> dataSource =  env.fromElements("s", "z", "x");

        DataSet<String> result = dataSource.map(new RichMapFunction<String, String>() {
            List<HashMap<String, Integer>> list = new ArrayList<>();
            HashMap<String, Integer> map = new HashMap<>();

            /**
             * 只会执行一次
             * @param parameters
             * @throws Exception
             */
            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                // 获取广播数据
                this.list = getRuntimeContext().getBroadcastVariable("broadcastName");
                for (HashMap m : list) {
                    map.putAll(m);
                }
            }

            @Override
            public String map(String value) throws Exception {
                Integer age = map.get(value);
                return value + ": " + age;
            }
        })
                // 执行广播数据
                .withBroadcastSet(toBroadcast, "broadcastName");

        result.print();
    }
}
